package com.example.springboot.utils;

import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;

import java.io.*;

public class ExcelToPdf {
	public static void main(String[] args) {
		convert2PDF("D:\\topdftest\\excel.xls", "D:\\topdftest\\excel.pdf");
	}

	protected static boolean matchLicense() {
		InputStream is = ExcelToPdf.class.getClassLoader().getResourceAsStream("license.xml");
		boolean result = false;
		License excelLicense = new License();
		try {
			excelLicense.setLicense(is);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void convert2PDF(String inPath, String outPath) {
		// 验证License
		if (!matchLicense()) {
			return;
		}
		File sourceFile = null;
		sourceFile = new File(inPath);
		File targetFile = null;
		targetFile = new File(outPath);
		FileOutputStream fileOs = null;
		Workbook wb = null;
		try {
			fileOs = new FileOutputStream(targetFile);
			wb = new Workbook(sourceFile.getAbsolutePath());
			wb.save(fileOs, SaveFormat.PDF);
		} catch (FileNotFoundException e) {
			// log.error("输出到" + sourceFile.getAbsolutePath() + "错误:" + e);
			e.getStackTrace();
		} catch (Exception e) {
			// log.error("转换word出错:" + e);
		} finally {
			if (fileOs != null) {
				try {
					fileOs.close();
				} catch (IOException e) {
					// log.error("关闭输出流出错:" + e);
				}
			}
		}
	}
}
